import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener
{
	public static Login app;
	// ������Ҫ�����
    private static JButton btn_login, btn_register;//��ť
    private JTextField ID_text;//�ı���
    private JPasswordField password_text;//�����

    public Login()
    {
    	// �������Ե�����
        setTitle("��½��");// ���ڱ���
        setSize(300, 180);// ���ڴ�С
        setLocationRelativeTo(null);// ���ھ���
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// �رմ��������ٵ�ǰ����
        setLayout(new FlowLayout());// ���ò�����ʽ����

        JPanel jp = new JPanel(new GridLayout(4, 1));// �������Ϊ��񲼾�4��1��
        // ��һ��
        JPanel jp1 = new JPanel();
        JLabel id_label = new JLabel("�˺� ");
        ID_text = new JTextField(12);
        jp1.add(id_label);
        jp1.add(ID_text);
        jp.add(jp1);
        // �ڶ���
        JPanel jp2 = new JPanel();
        JLabel password_label = new JLabel("���� ");
        password_text = new JPasswordField(12);
        jp2.add(password_label);
        jp2.add(password_text);
        jp.add(jp2);
        // ������
        JPanel jp3 = new JPanel();
        btn_login = new JButton("��¼");
        btn_login.addActionListener(this);       //������¼��ť
        btn_register = new JButton("ע��");
        btn_register.addActionListener(this);      //����ע�ᰴť
        jp3.add(btn_login);
        jp3.add(btn_register);
        jp.add(jp3);
        // ������
        JPanel jp4 = new JPanel();
        jp.add(jp4);

        add(jp);
    }

    public static void main(String[] args)
    {
    	app =new Login();
    	app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   	    app.setVisible(true);
    }

    @SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e)
    {
    	String cmd = e.getActionCommand();
    	if(cmd.equals("��¼"))
    	{
    		Database db = new Database("chatdata");
    		ResultSet rs = db.select("user_info", "*");

    		try
    		{
				while(rs.next())
				{
					if(rs.getString("id").toString().equals(ID_text.getText()))
					{
						if(rs.getString("password").toString().equals(password_text.getText()))
						{
							this.dispose();
							User app = new User();
							app.setSize(600,600);
							app.setVisible(true);
							return;
						}
						else
						{
							JOptionPane.showMessageDialog(this, "���벻��ȷ","��ʾ",JOptionPane.YES_OPTION);
							return;
						}
					}
					else
					{
						JOptionPane.showMessageDialog(this, "�û�������","��ʾ",JOptionPane.YES_OPTION);
						return;
					}
				}
			}
    		catch (SQLException e1) {
				// TODO �Զ����ɵ� catch ��
				e1.printStackTrace();
			}
    	}
    	else if(cmd.equals("ע��"))
    	{
    		Register app = new Register();
//    		app.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
       	    app.setVisible(true);
    	}
    }
}
